﻿//多线程发布
//执行任务的进口函数
function TaskRun(url, id) {
    $.ajax({
        type: "POST",
        url: url,
        success: function (msg) {
            taskData = eval("taskData=" + msg);
            if (taskData.result == "1") {
                TaskRunTutiThread.TaskInfoShowBegin(taskData);
                TaskRunTutiThread.ExecuteAllTask(taskData);
            }
        }
    });
}
//左侧树右键点击的ajax调用
function TaskRun_Ajax(url, id) {
    $.ajax({
        type: "POST",
        url: url,
        success: function (msg) {
            taskData = eval("taskData=" + msg);
            if (taskData.result == "1") {
                TaskRunTutiThread.LeftTreeTask(taskData);
            }
        }
    });
}
//多线程发布页面 封装类
var TaskRunTutiThread = {
    TaskWatchTimeOut: 100, //任务监视事件间隔(100毫秒)
    taskId: 0, //任务ID，仅作为标识使用而已
    blnKeepWatch: false, //是否在监视多线程发布
    ConcelAllTask: false,
    TaskInfoShowBegin: function (taskDataTemp) {//总任务的提示
        if (taskDataTemp) {
            var ac = taskDataTemp.ac;
            taskmag = "";
            switch (ac) {
                case "NodePub":
                    taskmag = "站点批量发布开始...";
                    break;
                case "RePubNode":
                    taskmag = "首页刷新开始...";
                    break;
                case "NodeRefresh":
                    taskmag = "站点批量更新开始...";
                    break;
                case "repubcontent":
                    taskmag = "批量更新开始...";
                    break;
                case "pubcontent":
                    taskmag = "批量发布开始...";
                    break;
                default:
                    taskmag = "执行操作开始执行...";
                    break;
            }
            TaskRunTutiThread.taskId++; //ID自增1
            top.set_statewin_show(); //当执行任务时显示信息查看窗口,common_win_function.js
            //TaskRunTutiThread.AddInfoForIframe(taskmag, 0); //执行窗口增加信息提示
        }
    },
    ExecuteAllTask: function (taskDataTemp) {//执行总的发布任务,将总的发布任务分解下，而后传递至后台进行发布
        if (typeof (taskDataTemp) == 'undefined' || typeof (taskDataTemp.items) == 'undefined' || typeof (taskDataTemp.ac) == 'undefined') {
            return;
        }
        if (TaskRunTutiThread.ConcelAllTask) {
            TaskRunTutiThread.ConcelAllTask = false;
        }
        if (taskDataTemp.contentbln == "1") {//是否更新内容
            TaskRunTutiThread.ExecutePubContent(taskDataTemp);
        }
        if (taskDataTemp.extrabln == "1") {//是否更新附加发布
            TaskRunTutiThread.ExecutePubExtra(taskDataTemp);
        }
        if (taskDataTemp.indexbln == "1") {//是否更新首页
            TaskRunTutiThread.ExecutePubIndex(taskDataTemp);
        }
    },
    ExecutePubContent: function (taskDataTemp) {//更新内容
        var _ContentCount = 0;
        var _Onceoper = taskDataTemp.onceoper; //单次最多处理的页数
        var _ArrTask = new Array(); //总的内容任务的数组
        for (var nodeid in taskDataTemp.items) {
            if (taskDataTemp.items[nodeid] != "") {
                var _contents = taskDataTemp.items[nodeid].contents;
                var _arrIds = _contents.split(',');
                var _groupIds = "";
                for (var k in _arrIds) {
                    _groupIds += _arrIds[k] + ',';
                    if ((_ContentCount + 1) % _Onceoper == 0) {
                        _groupIds = _groupIds.substring(0, _groupIds.length - 1);
                        taskInfo = new Object();
                        taskInfo.ac = "content";
                        taskInfo.url_data = "&contentid=" + _groupIds + "&t=" + Math.random();
                        //taskInfo.begin = "更新内容页 (" + (_ContentCount - _Onceoper + 1) + " , " + (_ContentCount + 1) + ")   (" + taskDataTemp.items[nodeid].nodename + ") ";
                        taskInfo.begin = "更新内容页 ( ID: " + _groupIds + ")   (" + taskDataTemp.items[nodeid].nodename + ") ";
                        TaskRunTutiThread.AjaxAddPubTask(taskInfo);
                        _groupIds = "";
                    }
                    _ContentCount++;
                }
                if (_groupIds != "") {
                    _groupIds = _groupIds.substring(0, _groupIds.length - 1);
                    var _tasksBeginIndex = 0;
                    var _tasksEndIndex = 0;
                    if (_ContentCount > _Onceoper) {
                        _tasksBeginIndex = (_ContentCount + 1) - _Onceoper;
                        _tasksEndIndex = _ContentCount + 1;
                    } else {
                        _tasksBeginIndex = 0;
                        _tasksEndIndex = _Onceoper - 1;
                    }
                    taskInfo = new Object();
                    taskInfo.ac = "content";
                    taskInfo.url_data = "&contentid=" + _groupIds + "&t=" + Math.random();
                    //taskInfo.begin = "更新内容页 (" + _tasksBeginIndex + " , " + _tasksEndIndex + ")   (" + taskDataTemp.items[nodeid].nodename + ")  ";
                    taskInfo.begin = "更新内容页 ( ID: " + _groupIds + ")   (" + taskDataTemp.items[nodeid].nodename + ")  ";
                    TaskRunTutiThread.AjaxAddPubTask(taskInfo);
                    _groupIds = "";
                }
            }
        }
    },
    ExecutePubExtra: function (taskDataTemp) {//附加发布的任务
        var items = taskDataTemp.items;
        for (var nodeid in items) {
            if (items[nodeid].extras != "") {
                //AddUpdateExtraPageTask(items[nodeid].extras.split(','), nodeid, items[nodeid].nodename, taskData.listpagecount); //添加附加发布的任务
                var arrExtraIds = items[nodeid].extras.split(',');
                for (var k in arrExtraIds) {
                    taskInfo = new Object();
                    taskInfo.ac = "Extra";
                    taskInfo.url_data = "&index=1&nodeid=" + nodeid + "&countNum=" + taskDataTemp.listpagecount + "&Extras=" + arrExtraIds[k] + "&t=" + Math.random();
                    taskInfo.begin = "更新 [" + nodename + "] (" + k + ")附加发布  ";
                    TaskRunTutiThread.AjaxAddPubTask(taskInfo);
                }
            }
        }
    },
    ExecutePubIndex: function (taskDataTemp) {//更新首页
        //AddUpdateIndexPageTask(taskData, nodeid, items[nodeid].nodename); //添加更新首页的任务    
        var CountNum = taskDataTemp.listpagecount;
        var items = taskDataTemp.items;
        for (var nodeid in items) {
            taskInfo = new Object();
            taskInfo.ac = "node";
            taskInfo.url_data = "&index=1&nodeid=" + nodeid + "&countNum=" + CountNum + "&t=" + Math.random(); ;
            taskInfo.begin = "更新 [" + items[nodeid].nodename + "] 首页 ";
            TaskRunTutiThread.AjaxAddPubTask(taskInfo);
        }
    },
    LeftTreeTask: function (taskData) {//左侧树的页面发布操作
        if (TaskRunTutiThread.ConcelAllTask) {
            TaskRunTutiThread.ConcelAllTask = false;
        }
        var items = taskData.items;
        for (var id in items) {
            var nodeinfo = items[id];
            var taskRun = new LeftTreeTaskControl(nodeinfo);
            taskRun.ExecuteTask();
        }
    },
    AjaxAddPubTask: function (taskInfoTemp) {//异步添加单个任务
        //taskInfoTemp.ac   taskInfoTemp.url_data   taskInfoTemp.begin
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        top.set_statewin_show(); //当执行任务时显示信息查看窗口,common_win_function.js
        setTimeout(function () {
            $.ajax({
                type: "POST",
                url: "publish/admin_taskexec.aspx",
                data: 'ac=' + taskInfoTemp.ac + taskInfoTemp.url_data,
                async: true,
                error: function () {
                    TaskRunTutiThread.AddInfoForIframe(taskInfoTemp.begin, 0, "【添加任务失败】");
                },
                success: function (msg) {
                    try {
                        msg = eval("msg=" + msg);
                    }
                    catch (error) {
                        TaskRunTutiThread.AddInfoForIframe(taskInfoTemp.begin, 0, "【添加任务失败】");
                    }
                    if (msg.result == "1") {
                        TaskRunTutiThread.AddInfoForIframe(taskInfoTemp.begin, msg.pubId);
                    }
                    if (TaskRunTutiThread.blnKeepWatch == false) {//设置监视器，监视页面发布情况
                        setTimeout(function () { TaskRunTutiThread.WatchFunc(); }, TaskRunTutiThread.TaskWatchTimeOut);
                        TaskRunTutiThread.blnKeepWatch = true;
                    }
                }
            });
        }, 500);
    },
    AddInfoForIframe: function (data, id, data2) { //执行窗口增加信息提示
        statewin.addInfoNewStyle(data, id, data2);
    },
    WatchFunc: function () {
        $.ajax({
            cache: false,
            url: "publish/admin_task_watch.ashx",
            data: 'ac=CheckTask',
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert(window.location);
                //alert(XMLHttpRequest + textStatus + errorThrown);
            },
            success: function (msg) {
                msg = eval("msg=" + msg);
                items = msg.items;
                for (var nodeid in items) {
                    TaskRunTutiThread.StatewinSetInfo(nodeid, items[nodeid]);
                    //statewin.SetInfo(nodeid, items[nodeid]);
                }
            },
            complete: function () {//再次等待一定的时间后监控
                setTimeout(function () { TaskRunTutiThread.WatchFunc(); }, TaskRunTutiThread.TaskWatchTimeOut);
            }
        });
    },
    StatewinSetInfo: function (nodeid, info) {
        statewin.SetInfo(nodeid, info);
    },
    CancelMyTask: function () {
        $.ajax({
            cache: false,
            url: "publish/admin_task_watch.ashx",
            data: 'ac=CancelMyTask',
            success: function (msg) {
                if (msg == "true") {
                    alert("所有任务已取消");
                    TaskRunTutiThread.ConcelAllTask = true; //取消所有任务
                    statewin.ClearAllInfo();
                }
                else {
                    alert("没有可以取消的任务");
                }
            }
        });
    }
};
function LeftTreeTaskControl(nodeinfo) {
    this.pageSize = 0;
    this.totalPage = 10;
    this.currtentPage = 1;
    this.ids = '';
    this._index = 1;
    this.index_num = 0;
    this.ExecuteTask = function () {
        var self = this;
        if (nodeinfo.ac == 'refresh') {//结点更新操作
            self.pageSize = nodeinfo.onceOper;
            if (nodeinfo.refresh_content == "1") {
                self.get_refersh_contentids(); //更新内容
            }
            else if (nodeinfo.refresh_extra == "1") {
                self.get_refersh_extraids(); //更新附加发布
            }
            else if (nodeinfo.refresh_index == "1") { //更新首页
                self.refersh_index();
            }
        }
        else if (nodeinfo.ac == 'nodepub') { //结点发布操作
            self.pageSize = nodeinfo.onceOper;
            self.get_nodepub_contentids(); //发布内容
        }
    };
    this.get_refersh_contentids = function () {//分页获取更新的ids
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        if (self.currtentPage <= self.totalPage) {
            self.index_num = (self.currtentPage - 1) * self.pageSize + 1;
            top.set_statewin_show(); //当执行任务时显示信息查看窗口,common_win_function.js
            //TaskRunTutiThread.AddInfoForIframe("更新内容(" + self.index_num + "," + (self.currtentPage * self.pageSize) + ")（" + nodeinfo.name + "） ", 0); //执行窗口增加信息提示
            $.ajax({
                type: "POST",
                url: "publish/admin_taskanalysis.aspx?ac=ajax_contentrefresh&nodeid=" + nodeinfo.nodeid + "&pageSize=" + nodeinfo.onceOper + "&currtentPage=" + self.currtentPage + "&t=" + Math.random(),
                async: true,
                error: function () {
                    TaskRunTutiThread.AddInfoForIframe('获取内容信息id(' + ((self.currtentPage - 1) * self.pageSize + 1) + ',' + (self.currtentPage * self.pageSize) + ')（' + nodeinfo.name + '）出错', 0);
                    self.currtentPage++;
                    self.get_refersh_contentids();
                },
                success: function (msg) {
                    var result = eval("result=" + msg);
                    self.totalPage = result.totalPage;
                    self.ids = result.ids;
                    self.refersh_content();
                }
            });
        }
        else {
            self.get_refersh_extraids();
        }
    };
    this.refersh_content = function () {//更新内容页
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        self.index_num = (self.currtentPage - 1) * self.pageSize + 1;
        taskInfo = new Object();
        taskInfo.ac = "content";
        taskInfo.url_data = "&contentid=" + self.ids;
        taskInfo.begin = "更新内容 (" + self.index_num + "," + (self.currtentPage * self.pageSize) + ")（" + nodeinfo.name + "） ";
        TaskRunTutiThread.AjaxAddPubTask(taskInfo);
        self.currtentPage++;
        self.get_refersh_contentids();
    };
    this.get_refersh_extraids = function () {
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        if (nodeinfo.refresh_extra == "1") {
            $.ajax({
                type: "POST",
                url: "publish/admin_taskanalysis.aspx?ac=ajax_extrarefresh&nodeid=" + nodeinfo.nodeid + "&t=" + Math.random(),
                error: function () {
                    self.ids = "";
                    TaskRunTutiThread.AddInfoForIframe('获取附加发布信息id（' + nodeinfo.name + '）出错', 0);
                },
                success: function (msg) {
                    var result = eval("result=" + msg);
                    self.ids = result.ids;
                    self.refersh_extra();
                }
            });
        }
        else {
            self.refersh_index();
        }
    };
    this.refersh_extra = function () {
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        taskInfo = new Object();
        taskInfo.ac = "Extra";
        taskInfo.url_data = "&Extras=" + self.ids + '&index=1&countNum=10&nodeid=' + nodeinfo.nodeid + '&t=' + Math.random();
        taskInfo.begin = "更新附加发布页（" + nodeinfo.name + "）";
        TaskRunTutiThread.AjaxAddPubTask(taskInfo);
        this.refersh_index();
    };
    this.get_nodepub_contentids = function () {//发页获取要发布的信息ids
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        if (self.currtentPage <= self.totalPage) {
            self.index_num = (self.currtentPage - 1) * self.pageSize + 1;
            $.ajax({
                type: "POST",
                url: "publish/admin_taskanalysis.aspx?ac=ajax_contentpub&nodeid=" + nodeinfo.nodeid + "&pageSize=" + nodeinfo.onceOper + "&currtentPage=" + self.currtentPage + "&t=" + Math.random(),
                error: function () {
                    TaskRunTutiThread.AddInfoForIframe('获取内容信息id(' + ((self.currtentPage - 1) * self.pageSize + 1) + ',' + (self.currtentPage * self.pageSize) + ')（' + nodeinfo.name + '）出错', 0);
                },
                success: function (msg) {
                    var result = eval("result=" + msg);
                    self.totalPage = result.totalPage;
                    self.ids = result.ids;
                    self.nodepub_content();
                }
            });
        }
        else {
            TaskRunTutiThread.AddInfoForIframe("结点 [" + nodeinfo.name + "] 内容发布任务添加完成", 0);
            self.refersh_index();
        }
    };
    this.nodepub_content = function () { //发布内容
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        var self = this;
        self.index_num = (self.currtentPage - 1) * self.pageSize + 1;
        taskInfo = new Object();
        taskInfo.ac = "content";
        taskInfo.url_data = "&contentid=" + self.ids;
        taskInfo.begin = "发布内容(" + self.index_num + "," + (self.currtentPage * self.pageSize) + ")（" + nodeinfo.name + "）";
        TaskRunTutiThread.AjaxAddPubTask(taskInfo);
        self.currtentPage++;
        self.get_nodepub_contentids();
    };
    this.refersh_index = function () { //刷新首页
        if (TaskRunTutiThread.ConcelAllTask)
            return;
        if (nodeinfo.refresh_index == "1") {
            taskInfo = new Object();
            taskInfo.ac = "node";
            taskInfo.url_data = '&index=1&countNum=20&nodeid=' + nodeinfo.nodeid;
            taskInfo.begin = "更新首页 (" + nodeinfo.name + ")";
            TaskRunTutiThread.AjaxAddPubTask(taskInfo);
        }
    };
}
//重新刷新窗体，会给出提示
var sp_isRefash = false;
function window_onbeforeunload() {
    if (!sp_isRefash) {
        var str = "";
        str = "如果关闭窗口会导致您断开与服务器的连接，您的很多请求所反馈的结果将不能实时反馈给浏览器，但不影响任务的执行，您真的要退出系统吗？";
        return str;
    }
}